/**
 * Created by yangfan on 2016/12/7.
 */
/**
 * Created by yangfan on 2016/11/23.
 */
define(['config/appregister',
        "i18n",
        "jquery",
        "business/myResume/services/httpServices",
        "business/services/utils",
    ],
    function(app,i18n,$,httpServices,utils,constant){
        app.directive("jobIntension",function(){
            return {
                templateUrl:"app/business/myResume/templates/jobIntension.html",
                scope:{
                    conf:"=confData"
                },
                link:function(scope,element,attrs){
                    scope.i18n = i18n;
                    scope.curI18n = i18n.resume;
                    scope.validate = {
                        IDCard:"/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/",
                        number:"/^\d{3}$/"
                    };

                    var originOption = "请选择";

                    function getValidateArr(form,topKey){
                        if(!angular.isObject(form)){
                            return ;
                        };
                        var itemArr = [];
                        for(var key in form ){
                            if(key.indexOf("$")<0){
                                itemArr.push(key);
                            }
                        }
                        return  getValidateItem(topKey,itemArr);
                    }
                    function getValidateItem(topKey,itemArr){
                        if( toString.call(itemArr) != '[object Array]'){
                            return
                        }
                        var flag =false;
                        itemArr.forEach(function(item ){
                            if(scope[topKey][item].$invalid == true){
                                scope[topKey][item].$pristine = false;
                                flag = true;
                            }
                        });
                        return flag ;
                    }
                    function getMultiRegion(Arr){
                        if( toString.call(Arr) != '[object Array]'){
                            return
                        };
                        var reArr = [];
                        Arr.forEach(function(item,index,array){
                            var obj = {
                                ewpProvinceId:item.provinceId,
                                ewpRegionId:item.regionId,
                                ewpAreaId:item.areaId
                            };
                            reArr.push(obj);
                        });
                        reArr = JSON.stringify(reArr);
                        return reArr ;
                    }
                    /*
                     * getMulti函数 作用为将Arr数组中与obj 对象对应的属性赋值给obj，最后obj 子属性返回字符串   @Arr @obj  ！
                     * */
                    function getMulti(Arr,obj){
                        if( toString.call(Arr) != '[object Array]'||toString.call(obj) != '[object Object]'){
                            return ""
                        };
                        Arr.forEach(function(item,index,array){
                            for(var key in obj ){
                                obj[key].push(item[key]);
                            }
                        });
                        for(var key in obj ){
                            obj[key] = obj[key].join(",") ;
                        }

                        return obj ;
                    }

                    //基本信息
                    scope.isShow = {
                        //求职状态
                        resumeCondition:false,
                        showResumeConTips:false
                    };
                    var selectStyleSmall = {"background-color":"#fff", "width":"90px"},
                        selectStyleBig = {"background-color":"#fff", "width":"195px"};

                    /*
                     * 求职意向
                     * */
                    scope.showComponent = function(key){
                        scope[key].isShow = !scope[key].isShow ;
                    };

                    scope.leaveCalandar = function(){
                        scope.jobInArrivalTime.isShow = false ;
                    };
                    scope.jobConf = {
                        top:"",
                        detail:"",
                        isShow:false
                    };
                    scope.majorConf = {
                        top:"",
                        second:"",
                        detail:"",
                        isShow:false
                    };
                    scope.jobIntentionSelect = {
                        salary:{
                            model:"",
                            value:scope.curI18n.placeholderSelect,
                            key:"srName",
                            style:selectStyleBig
                        },
                        jobNature:{
                            model:"",
                            value:scope.curI18n.placeholderSelect,
                            key:"dictName",
                            style:selectStyleBig
                        },
                        errMessage:{
                            salary:false
                        }



                    };
                    //期望工作地
                    scope.jobInRegionIntension = {
                        reValue:[],
                        isShow:false,
                        showValue:"",
                        maxSelect:3,

                    };
                    //职能
                    scope.jobInJobConf = {
                        top:"",
                        detail:"",
                        showValue:"",
                        isShow:false,
                        maxSelect:3

                    };
                    //行业
                    scope.jobInIndustryConf = {
                        detail:"",
                        showValue:"",
                        isShow:false,
                        maxSelect:3
                    };
                    scope.jobInArrivalTime = {
                        value:"",
                        isShow:false,

                    };
                    function _isValidateSelect(){
                        scope.jobIntentionSelect.errMessage.salary = false;
                        if(scope.jobIntentionSelect.salary.value== scope.curI18n.placeholderSelect){
                            scope.jobIntentionSelect.errMessage.salary = true;
                            return true
                        }else{
                            return false
                        }
                    }
                    scope.jobIntentionSubmit = function(){
                        if(scope.jobIntention.$invalid ||_isValidateSelect()){
                           var  topKey = "jobIntention";
                            var flag = getValidateArr(scope.jobIntention,topKey);
                            //期望薪资
                            if(scope.jobIntention.$invalid){
                                _isValidateSelect()
                            }
                        }else{
                            var obj = getMultiRegion(scope.jobInRegionIntension.reValue);
                            var jobInConf = {
                                    fdcId:[]
                                },
                                industryObj = {
                                    dictId:[]
                                };
                            var funcs = getMulti(scope.jobInJobConf.detail,jobInConf);
                            var industry = getMulti(scope.jobInIndustryConf.detail,industryObj);
                            var req = {
                                type:"POST",
                                uri:"saveJobIntention",
                                data:{
                                    salaryId:scope.jobIntentionSelect.salary.value.srId,
                                    //ewpProvinceId:obj.ewpProvinceId,
                                    //ewpRegionId:obj.ewpRegionId,
                                    //ewpAreaId:obj.ewpAreaId,
                                    //expectWorkPlace:"UI没有写详细位置",
                                    ewpJsonStr:obj,

                                    functionsIds:funcs.fdcId,

                                    // not required
                                    arrivalTime:utils.YMD2YMD(scope.jobInArrivalTime.value),
                                    natureId:(!scope.jobIntentionSelect.jobNature.value || scope.jobIntentionSelect.jobNature.value ==scope.curI18n.placeholderSelect)?"":scope.jobIntentionSelect.jobNature.value.dictId,
                                    guildIds:industry == ""?"":industry.dictId
                                }
                            };
                            httpServices.getCvdatabases(req).success(function(response){
                                if(response.msg == "success"){
                                    var obj = {
                                        type:0,
                                        key:"jobIntension"
                                    };
                                    scope.$emit("backToResume",obj);

                                }
                            }).error(function(response){
                                scope.$parent.showBox.jobIntension = [];
                                console.log("Error:" + response.msg)
                            });
                        }
                    };
                    scope.cancelSubmit = function(){
                        scope.$emit("backToResume",{type:1,key:"jobIntension"});
                    };

                    function fillData(){
                        //这里在数据中间层单独赋值  不改变引用组件数据
                        //薪资
                        scope.jobIntentionSelect.salary.value = scope.conf.salary || scope.curI18n.placeholderSelect;
                        //scope.jobIntentionSelect.salary.value.srId = scope.conf.salary.srId;

                        //职能
                        scope.jobInJobConf.detail = scope.conf.jobConf;
                        scope.jobInJobConf.showValue = utils.getJobConf(scope.conf.jobConf);

                        //到岗时间
                        scope.jobInArrivalTime.value =  utils.getYMD(scope.conf.jobInArrivalTime);

                        //行业
                        scope.jobInIndustryConf.detail =  scope.conf.jobInIndustryConf;
                        scope.jobInIndustryConf.showValue =  utils.getIndustry(scope.conf.jobInIndustryConf);

                        //工作性质
                        scope.jobIntentionSelect.jobNature.value =  scope.conf.jobNature;

                        //期望工作地
                        scope.jobInRegionIntension.reValue =  utils.ew2ReValue(scope.conf.jobInRegionIntension);
                        scope.jobInRegionIntension.showValue =  utils.getIntentionRegion(scope.conf.jobInRegionIntension);

                        initJobIntension();

                    }

                    function getSalaryCount(){
                        var req = {
                            type:"GET",
                            uri:"getSalaryCount"
                        };
                        httpServices.getCvdatabases(req).success(function(response){
                            scope.$apply(function(){
                                scope.jobIntentionSelect.salary.model = response.data;
                            })
                        }).error(function(){})
                    }
                    function getJobNature(){
                        var req = {
                            type:"GET",
                            uri:"getJobNature"
                        };
                        httpServices.jobServices(req).success(function(response){
                            scope.$apply(function(){
                                scope.jobIntentionSelect.jobNature.model = response.data;
                            })
                        }).error(function(){})
                    }
                    //初始化求职意向
                    function initJobIntension(){
                        getSalaryCount();
                        getJobNature();
                    }
                    //item 为标记参数
                    scope.workExpression = ["item"];
                    fillData();
                }
            }
        })
    });